package com.zjj.edu.framework.util;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

public class UserPermissionUtil {


    /**
     * 返回用户租户编号 - 如果为平台租户则设为-1
     * @param tenantId
     * @return
     */
    public static long getExcludeAdminUserTenantId(Object tenantId) {
        Subject subject = SecurityUtils.getSubject();
        long loginTenantId = ShiroUtils.getSysUser().getTenantId();
        if (tenantId == null && loginTenantId == 1) {
            return -1;
        }
        boolean flag = true;
        try {
            subject.checkPermission("system:superAdmin:opt");
        } catch (Exception e) {
            flag = false;
        }
        if (flag) {
            // 平台管理员操作
            return Long.parseLong(tenantId + "");
        } else {
            return loginTenantId;
        }
    }

    /**
     * 返回用户租户编号
     * @param tenantId
     * @return
     */
    public static long getUserTenantId(Object tenantId) {
        Subject subject = SecurityUtils.getSubject();
        long loginTenantId = ShiroUtils.getSysUser().getTenantId();
        if (tenantId == null) {
            return loginTenantId;
        }
        boolean flag = true;
        try {
            subject.checkPermission("system:superAdmin:opt");
        } catch (Exception e) {
            flag = false;
        }
        if (flag) {
            // 平台管理员操作
            return Long.parseLong(tenantId + "");
        } else {
            return loginTenantId;
        }
    }

    /**
     *
     * @return
     */
    public static boolean checkUserPermiss() {
        Subject subject = SecurityUtils.getSubject();
        boolean flag = true;
        try {
            subject.checkPermission("system:superAdmin:opt");
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}
